package com.wyzz.hr.io.extend;

import java.math.BigDecimal;
import java.util.*;
import org.apache.commons.beanutils.PropertyUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.wyzz.hr.bo.IEmpSalaryAcctBo;
import com.wyzz.hr.bo.IEmpSalaryAcctitemsBo;
import com.wyzz.hr.bo.IEmployeeBo;
import com.wyzz.hr.bo.IJobgradeBO;
import com.wyzz.hr.bo.ISalaryconfBo;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.Empsalaryacct;
import com.wyzz.hr.pojo.Empsalaryacctitems;
import com.wyzz.hr.pojo.Empsalaryacctversion;
import com.wyzz.hr.pojo.Empsalaryconfig;
import com.wyzz.hr.pojo.Jobgrade;
import com.wyzz.hr.spring.SpringBeanFactory;
import com.wyzz.hr.util.DatabaseSysConfigManager;
import com.wyzz.hr.util.SysConfigManager;

public class IEmpSalaryConfig extends ICheckAndInsert {

	public IEmpSalaryConfig() {
		msgDbInsertError = "\u6570\u636E\u5E93\u63D2\u5165\u5931\u8D25\uFF01";
		msgGreaterThan = "{0}\u5927\u4E8E{1}\uFF01";
		msgExist = "{0}\u5DF2\u5B58\u5728\uFF01";
		msgNoNull = "{0}\u4E0D\u80FD\u4E3A\u7A7A\uFF01";
		msgNotExist = "{0}\u4E0D\u5B58\u5728\uFF01";
		msgNotExistOrQuit = "{0}\u4E0D\u5B58\u5728\u6216\u5DF2\u7ECF\u79BB\u804C\uFF01";
		msgNotExistOrStop = "{0}\u4E0D\u5B58\u5728\u6216\u5DF2\u7ECF\u88AB\u505C\u7528\uFF01";
		msgNumLimit = "\u5BFC\u5165\u5458\u5DE5\u6570\u76EE\u8D85\u8FC7\u9650\u5236\uFF01";
		msgCanNotQuit = "\u8BE5\u5458\u5DE5\u6709\u4E0B\u5C5E\u5458\u5DE5\uFF0C\u65E0\u6CD5\u79BB\u804C\uFF01";
		msgQuitNeedInfo = "\u79BB\u804C\u4EBA\u5458\u5FC5\u987B\u586B\u5199\u79BB\u804C\u76F8\u5173\u4FE1\u606F\uFF01";
		msgEmpCircle = "\u51FA\u73B0\u5458\u5DE5\u73AF\uFF0C\u5BF9\u5E94\u7684\u884C\u6570\u4E3A\uFF1A{0}\uFF01";
		msgDbRepeat = "\u6570\u636E\u5E93\u6570\u636E\u51FA\u73B0\u91CD\u590D\uFF0C\u5BFC\u5165\u5931\u8D25\uFF01";
		salaryconfBo = (ISalaryconfBo) SpringBeanFactory
				.getBean("salaryconfBo");
	}

	public int[] insertTransmit(List insertList, CommonParameters commonParas)
			throws Exception {
		List originalList = insertList;
		Employee currEmp = commonParas.getCurrEmp();
		int result[] = { 0, 0 };
		SysConfigManager dbConfigManager = DatabaseSysConfigManager
				.getInstance();
		String updateConf = dbConfigManager
				.getProperty("sys.salary.config.update");
		Map jobgradeMap = getJobgradeMap();
		Map empsalaryacctMap = getEsaNameVersionMap();
		Map itemsMap = createAcctItemsMap();
		Map empDbMap = getEmployeeMap();
		Map escDbMap = new HashMap();
		escDbMap = getEmpsalaryconfigMap();
		List configList = new ArrayList();
		int rowNum = 1 - commonParas.getInmatchModel().getImmNoTitle()
				.intValue();
		Iterator i$ = originalList.iterator();
		do {
			if (!i$.hasNext())
				break;
			Empsalaryconfig newConfig = (Empsalaryconfig) i$.next();
			rowNum++;
			Employee employee = newConfig.getEmployee();
			if (employee == null || employee.getEmpDistinctNo() == null)
				commonParas.addErrorMessage(msgNoNull, Integer.valueOf(rowNum),
						new String[] { (new StringBuilder()).append(
								Empsalaryconfig.PROP_EMPLOYEE).append(".")
								.append(Employee.PROP_EMP_DISTINCT_NO)
								.toString() });
			else if (!empDbMap.containsKey(newConfig.getEmployee()
					.getEmpDistinctNo())) {
				commonParas.addErrorMessage(msgNotExist, Integer
						.valueOf(rowNum), new String[] { (new StringBuilder())
						.append(Empsalaryconfig.PROP_EMPLOYEE).append(".")
						.append(Employee.PROP_EMP_DISTINCT_NO).toString() });
			} else {
				Employee emp = (Employee) empDbMap.get(employee
						.getEmpDistinctNo());
				newConfig.setEmployee(emp);
				newConfig.setId(emp.getId());
			}
			Empsalaryconfig oldConfig = (Empsalaryconfig) escDbMap
					.get(newConfig.getId());
			if (oldConfig != null) {
				if ("0".equals(updateConf)) {
					commonParas.addMessage(msgDbRepeat, Integer.valueOf(1),
							Integer.valueOf(rowNum), new String[0]);
					result[1]++;
					continue;
				}
				oldConfig.decryEmpSalaryConf(oldConfig);
				commonParas.copy1To2(newConfig, oldConfig);
				newConfig = oldConfig;
			}
			Jobgrade jobgrade = newConfig.getEscJobgrade();
			if (jobgrade == null || jobgrade.getJobGradeName() == null)
				commonParas.addErrorMessage(msgNoNull, Integer.valueOf(rowNum),
						new String[] { (new StringBuilder()).append(
								Empsalaryconfig.PROP_ESC_JOBGRADE).append(".")
								.append(Jobgrade.PROP_JOB_GRADE_NAME)
								.toString() });
			else if (!jobgradeMap.containsKey(jobgrade.getJobGradeName()))
				commonParas.addErrorMessage(msgNotExist, Integer
						.valueOf(rowNum), new String[] { (new StringBuilder())
						.append(Empsalaryconfig.PROP_ESC_JOBGRADE).append(".")
						.append(Jobgrade.PROP_JOB_GRADE_NAME).toString() });
			else
				newConfig.setEscJobgrade((Jobgrade) jobgradeMap.get(jobgrade
						.getJobGradeName()));
			Empsalaryacctversion esav = newConfig.getEscEsavId();
			if (esav == null || esav.getEsavEsac() == null
					|| esav.getEsavEsac().getEsacName() == null)
				commonParas.addErrorMessage(msgNoNull, Integer.valueOf(rowNum),
						new String[] { (new StringBuilder()).append(
								"escEsavId.").append(
								Empsalaryacctversion.PROP_ESAV_ESAC)
								.append(".").append(
										Empsalaryacct.PROP_ESAC_NAME)
								.toString() });
			else if (!empsalaryacctMap.containsKey(esav.getEsavEsac()
					.getEsacName())) {
				commonParas.addErrorMessage(msgNotExist, Integer
						.valueOf(rowNum), new String[] { (new StringBuilder())
						.append("escEsavId.").append(
								Empsalaryacctversion.PROP_ESAV_ESAC)
						.append(".").append(Empsalaryacct.PROP_ESAC_NAME)
						.toString() });
			} else {
				newConfig.setEscEsavId((Empsalaryacctversion) empsalaryacctMap
						.get(esav.getEsavEsac().getEsacName()));
				newConfig.setAcctItems((List) itemsMap.get(newConfig
						.getEscEsavId().getId()));
			}
			if (newConfig.getEscBankAccountNo() == null)
				commonParas
						.addErrorMessage(
								msgNoNull,
								Integer.valueOf(rowNum),
								new String[] { Empsalaryconfig.PROP_ESC_BANK_ACCOUNT_NO });
			List acctItems = newConfig.getAcctItems();
			if (acctItems != null) {
				for (int i = 0; i < acctItems.size(); i++) {
					Integer isCalc = ((Empsalaryacctitems) acctItems.get(i))
							.getEsaiDataIsCalc();
					try {
						if (isCalc.intValue() == 1 || isCalc.intValue() == 2)
							PropertyUtils.setProperty(newConfig,
									(new StringBuilder()).append("escColumn")
											.append(i + 1).toString(),
									new BigDecimal("0.00"));
					} catch (Exception e) {
						e.printStackTrace();
					}
				}

			}
			configList.add(newConfig);
		} while (true);
		if (commonParas.getIoMessages().hasErrorMsg())
			return result;
		salaryconfBo.interpretConfig((Empsalaryconfig[]) configList
				.toArray(new Empsalaryconfig[configList.size()]));
		result[0] = configList.size();
		Empsalaryconfig newConfig;
		for (i$ = configList.iterator(); i$.hasNext(); salaryconfBo
				.saveOrUpdateEsc(newConfig, currEmp.getId(), newConfig
						.getEmployee()))
			newConfig = (Empsalaryconfig) i$.next();

		return result;
	}

	private Map getJobgradeMap() {
		Map jobgradeTable = new HashMap();
		IJobgradeBO jobgradeBO = (IJobgradeBO) SpringBeanFactory
				.getBean("jobgradeBO");
		List jobgradeBOList = jobgradeBO.FindAllJobgrade();
		for (int i = 0; i < jobgradeBOList.size(); i++) {
			Jobgrade jobgrade = (Jobgrade) jobgradeBOList.get(i);
			jobgradeTable.put(jobgrade.getJobGradeName(), jobgrade);
		}

		return jobgradeTable;
	}

	private Map getEsaNameVersionMap() {
		Map result = new HashMap();
		IEmpSalaryAcctBo esaBo = (IEmpSalaryAcctBo) SpringBeanFactory
				.getBean("empsalaryacctBo");
		List salaryAcctList = esaBo.searchAllEmpsalaryacctVersionInUse();
		Empsalaryacctversion esav;
		for (Iterator i$ = salaryAcctList.iterator(); i$.hasNext(); result.put(
				esav.getEsavEsac().getEsacName(), esav))
			esav = (Empsalaryacctversion) i$.next();

		return result;
	}

	private Map createAcctItemsMap() {
		Map result = new HashMap();
		IEmpSalaryAcctitemsBo empSalaryAcctitemsBo = (IEmpSalaryAcctitemsBo) SpringBeanFactory
				.getBean("empsalaryacctitemsBo");
		List acct = empSalaryAcctitemsBo.findAll();
		for (Iterator i$ = acct.iterator(); i$.hasNext();) {
			Empsalaryacctitems item = (Empsalaryacctitems) i$.next();
			String key = item.getEsaiEsav().getId();
			if (result.containsKey(key)) {
				((List) result.get(key)).add(item);
			} else {
				List tmpList = new ArrayList();
				tmpList.add(item);
				result.put(key, tmpList);
			}
		}

		return result;
	}

	private Map getEmpsalaryconfigMap() {
		Map result = new HashMap();
		List escDbList = salaryconfBo.getAllEmpsalaryconfig();
		Empsalaryconfig esc;
		for (Iterator i$ = escDbList.iterator(); i$.hasNext(); result.put(esc
				.getId(), esc))
			esc = (Empsalaryconfig) i$.next();

		return result;
	}

	public Map getEmployeeMap() {
		Map employeeHashtable = new HashMap();
		IEmployeeBo esaBo = (IEmployeeBo) SpringBeanFactory.getBean("empBo");
		DetachedCriteria dc = DetachedCriteria.forClass(Employee.class);
		dc.createAlias(Employee.PROP_EMP_BENEFIT_TYPE, "empBenefitType", 1);
		dc.createAlias(Employee.PROP_EMP_BENEFIT, "benefit", 1);
		dc.createAlias(Employee.PROP_EMP_DEPT_NO, "empDeptNo", 1);
		dc.createAlias(Employee.PROP_CONFIG, "config", 1);
		dc.add(Restrictions.eq(Employee.PROP_EMP_STATUS, Integer.valueOf(1)));
		List empList = esaBo.findByCriteria(dc);
		Employee emp;
		for (Iterator i$ = empList.iterator(); i$.hasNext(); employeeHashtable
				.put(emp.getEmpDistinctNo(), emp))
			emp = (Employee) i$.next();

		return employeeHashtable;
	}

	String msgDbInsertError;
	String msgGreaterThan;
	String msgExist;
	String msgNoNull;
	String msgNotExist;
	String msgNotExistOrQuit;
	String msgNotExistOrStop;
	String msgNumLimit;
	String msgCanNotQuit;
	String msgQuitNeedInfo;
	String msgEmpCircle;
	String msgDbRepeat;
	private ISalaryconfBo salaryconfBo;
}
